package dbms.ypr;

import java.io.File;
import java.util.List;
import java.util.Scanner;


public class Drop {

    private static String sep;     //分隔符
    private static String dbName;  //数据库名称
    private static String tableName;   //表名

    //对drop的具体对象及逆行分类
    public static void dropSql(String sql){
        //得到分隔符
        sep = dbms.ypr.SQLConstant.getSeparate();

        //如果是删除数据库的操作
        if(sql.contains(" database ")){
            dbName = sql.substring(14, sql.length()-1);
            //删除该数据库
            dropDatabase();
        }
        else if(sql.contains(" table ")){
            tableName = sql.substring(11, sql.length()-1);
            //删除该表
            dropTable();
        }
        else{
            System.out.println("ERROR: 无法识别该指令");
        }

    }


    private static void dropDatabase(){
        //获得当前路径
        String path = dbms.ypr.SQLConstant.getNowPath();
        //判断是否有该数据库
        List<String> dbList = dbms.ypr.Utils.getAllDatabase(path);

        boolean a = dbList.contains(dbName);
        if(a) {

            boolean b = confirm();
            if (b) {
                String nowPath = path + "\\" + dbName;
                deleteFile(new File(nowPath));
                System.out.println("Query OK");
            }
            else{
                System.out.println("撤回成功,数据库未被删除");
            }
        }
        else{
            System.out.println("ERROR: 该数据库不存在");
        }

        dbms.ypr.Input.get();
    }

    //删除文件夹
    public static boolean deleteFile(File dirFile) {
        // 如果dir对应的文件不存在，则退出
        if (!dirFile.exists()) {
            return false;
        }

        if (dirFile.isFile()) {
            return dirFile.delete();
        } else {

            for (File file : dirFile.listFiles()) {
                deleteFile(file);
            }
        }

        return dirFile.delete();
    }

    //确认是否删除
    private static boolean confirm(){
        System.out.println("确认删除: \"Yes\" or \"No\"");
        System.out.print("请输入: ");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine().trim().toLowerCase();
        if("yes".equals(input)){
            return true;
        }
        else if("no".equals(input)){
            return false;
        }
        else{
            System.out.println("输入的单词无法识别");
            return false;
        }
    }

    //删除数据表
    private static void dropTable(){
        //获得当前路径
        String path = dbms.ypr.SQLConstant.getNowPath();
        //判断是否有该数据表
        List<String> tableList = dbms.ypr.Utils.getAllTables(path);

        boolean a = tableList.contains(tableName);
        if(a) {

            boolean b = confirm();
            if (b) {
                String nowPath = path + "\\" + tableName + ".txt";
                File file = new File(nowPath);
                file.delete();
                System.out.println("Query OK");
            }
            else{
                System.out.println("撤回成功,数据库未被删除");
            }
        }
        else{
            System.out.println("ERROR: 该数据库不存在");
        }

        dbms.ypr.Input.get();
    }
}
